package com.webshop.sbm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by xuexue on 2014/4/14.
 */
@Controller
@RequestMapping("/baseController")
public class BaseController {
    private static final String prefixRedirect="/redirect/";

    public void toVm(Result result,Model model){
        model.addAttribute("success",result.getSuccess());
        model.addAttribute("msg",result.getMsg());
        model.addAllAttributes(result.getResult());
    }



    @RequestMapping(prefixRedirect+"**/{fileName}")
    public String redirect(@PathVariable String fileName,HttpServletRequest request,Model model){
        String redirectPath=request.getServletPath();
        Map<String,String[]> paramMap=request.getParameterMap();
        for(Map.Entry<String,String[]> entry:paramMap.entrySet()){
            model.addAttribute(entry.getKey(),entry.getValue()[0]);
        }
        return redirectPath.substring(redirectPath.indexOf(prefixRedirect)+prefixRedirect.length(),redirectPath.lastIndexOf(fileName)+fileName.length());
    }
}
